package 递归.二分查找;

public class Test {
    public static int search(int[] a, int target) {
        return f(a, target, 0, a.length - 1);
    }

    public static int f(int[] a, int target, int i, int j) {
        if (i > j) {
            System.out.print("找不到 ");
            return -1;
        }
        int m = i + j >>> 1;
        if (a[m] > target) {
            return f(a, target, i, j - 1);
        } else if (a[m] < target) {
            return f(a, target, i + 1, j);
        } else {
            return m;
        }
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        System.out.println(search(a, 1));
        System.out.println(search(a, 3));
        System.out.println(search(a, 7));
        System.out.println(search(a, 190));
    }
}
